/*
 *@Project:
 *@Author: wanghong
 *@Date: 2019年07月10日
 *@Copyright: ©2019-2028 www.xfxb.net Inc. All rights reserved.
 */
package com.dopper.ws;

import io.netty.channel.epoll.Epoll;

/**
 * @author wanghong
 * @date 2019年07月10日
 */
public class WebsocketServerConfig {

    /**
     * 业务线程池
     */
    private int serverWorkerThreads = 8;

    /**
     * 处理TCP链接的线程池
     */
    private int serverBossThreads = 1;

    /**
     * 处理IO事件的线程池
     */
    private int serverSelectorThreads = 3;

    /**
     * 超时检测 小于等于0时不做超时检测 大于等于0时以秒计时 超时自动关闭链接
     */
    private int serverChannelMaxIdleTimeSeconds = 0;

    private int serverSocketSndBufSize = 65535;

    private int serverSocketRcvBufSize = 65535;

    private boolean serverPooledByteBufAllocatorEnable = true;

    private int backlog = 1024;

    private boolean useEpollNativeSelector = true;

    private static final String OS_NAME = System.getProperty("os.name");

    private static boolean isLinuxPlatform = false;

    private boolean useSsl = false;

    /**
     * an input stream for an X.509 certificate chain in PEM format
     */
    private String sslCertPath;

    /**
     * an input stream for a PKCS#8 private key in PEM format
     */
    private String sslKeyPath;

    /**
     * the password of the {@code keyFile}, or {@code null} if it's not
     */
    private String sslKeyPassword;

    static {
        if (OS_NAME != null && OS_NAME.toLowerCase().contains("linux")) {
            isLinuxPlatform = true;
        }
    }

    public static boolean isIsLinuxPlatform() {
        return isLinuxPlatform;
    }

    public boolean useEpoll() {
        return isIsLinuxPlatform() && Epoll.isAvailable() && useEpollNativeSelector;
    }

    public boolean isUseSsl() {
        return useSsl;
    }

    public void setUseSsl(boolean useSsl) {
        this.useSsl = useSsl;
    }

    public String getSslCertPath() {
        return sslCertPath;
    }

    public void setSslCertPath(String sslCertPath) {
        this.sslCertPath = sslCertPath;
    }

    public String getSslKeyPath() {
        return sslKeyPath;
    }

    public void setSslKeyPath(String sslKeyPath) {
        this.sslKeyPath = sslKeyPath;
    }

    public String getSslKeyPassword() {
        return sslKeyPassword;
    }

    public void setSslKeyPassword(String sslKeyPassword) {
        this.sslKeyPassword = sslKeyPassword;
    }

    public boolean isUseEpollNativeSelector() {
        return useEpollNativeSelector;
    }

    public void setUseEpollNativeSelector(boolean useEpollNativeSelector) {
        this.useEpollNativeSelector = useEpollNativeSelector;
    }

    public static void setIsLinuxPlatform(boolean isLinuxPlatform) {
        WebsocketServerConfig.isLinuxPlatform = isLinuxPlatform;
    }

    public int getServerWorkerThreads() {
        return serverWorkerThreads;
    }

    public void setServerWorkerThreads(int serverWorkerThreads) {
        this.serverWorkerThreads = serverWorkerThreads;
    }

    public int getServerBossThreads() {
        return serverBossThreads;
    }

    public void setServerBossThreads(int serverBossThreads) {
        this.serverBossThreads = serverBossThreads;
    }

    public int getServerSelectorThreads() {
        return serverSelectorThreads;
    }

    public void setServerSelectorThreads(int serverSelectorThreads) {
        this.serverSelectorThreads = serverSelectorThreads;
    }

    public int getServerChannelMaxIdleTimeSeconds() {
        return serverChannelMaxIdleTimeSeconds;
    }

    public void setServerChannelMaxIdleTimeSeconds(int serverChannelMaxIdleTimeSeconds) {
        this.serverChannelMaxIdleTimeSeconds = serverChannelMaxIdleTimeSeconds;
    }

    public int getServerSocketSndBufSize() {
        return serverSocketSndBufSize;
    }

    public void setServerSocketSndBufSize(int serverSocketSndBufSize) {
        this.serverSocketSndBufSize = serverSocketSndBufSize;
    }

    public int getServerSocketRcvBufSize() {
        return serverSocketRcvBufSize;
    }

    public void setServerSocketRcvBufSize(int serverSocketRcvBufSize) {
        this.serverSocketRcvBufSize = serverSocketRcvBufSize;
    }

    public boolean isServerPooledByteBufAllocatorEnable() {
        return serverPooledByteBufAllocatorEnable;
    }

    public void setServerPooledByteBufAllocatorEnable(boolean serverPooledByteBufAllocatorEnable) {
        this.serverPooledByteBufAllocatorEnable = serverPooledByteBufAllocatorEnable;
    }

    public int getBacklog() {
        return backlog;
    }

    public void setBacklog(int backlog) {
        this.backlog = backlog;
    }
}
